package com.goodwill.androidtest.notifyItem;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.goodwill.androidtest.R;
import com.goodwill.androidtest.notifyItem.adapter.BRVAHMultiRvAdapter;
import com.goodwill.androidtest.notifyItem.adapter.BRVAHTestRvAdapter;

import java.util.LinkedList;
import java.util.List;

public class BRVAHTestActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView;
    private TextView tv_insert_data;
    private TextView tv_delete_data;
    private BRVAHTestRvAdapter mAdapter;
    private List<String> mDataList = new LinkedList<>();
    private List<MultiItemBean> mMultiDataList = new LinkedList<>();
    private BRVAHMultiRvAdapter mBRVAHMultiRvAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_brvah_test);
//        initView();
//        initData();
    }

    private void initView() {
        mRecyclerView = findViewById(R.id.recyclerView);
        tv_insert_data = findViewById(R.id.tv_insert_data);
        tv_delete_data = findViewById(R.id.tv_delete_data);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
//        mAdapter = new BRVAHTestRvAdapter();
//        mRecyclerView.setAdapter(mAdapter);
//        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
//            @Override
//            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                String item = (String) adapter.getItem(position);
//                item = "新条目" + position;
//                mAdapter.setData(position, item);
//            }
//        });

        mBRVAHMultiRvAdapter = new BRVAHMultiRvAdapter();
        mRecyclerView.setAdapter(mBRVAHMultiRvAdapter);

        tv_insert_data.setOnClickListener(v -> {
//            mAdapter.setData(2, "新条目2");
//            mAdapter.setData(10, "新条目10");
//            mAdapter.setData(20, "新条目20");
            for (int i = 1; i < 3; i++) {
                mBRVAHMultiRvAdapter.addData(i, new MultiItemBean(ViewType.TEXT.ordinal(), "新文本", "新文本" + i));
            }
        });
        tv_delete_data.setOnClickListener(v -> {
            for (int i = 1; i < 3; i++) {
                int removePosition = i + 1;
                mBRVAHMultiRvAdapter.remove(removePosition);
            }
        });
        mBRVAHMultiRvAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                MultiItemBean item = mBRVAHMultiRvAdapter.getItem(position);
                MultiItemBean itemRelation = mBRVAHMultiRvAdapter.getItem(item.getRelationId());
                itemRelation.setTitle("你变坏了:"+item.getTitle());
                mBRVAHMultiRvAdapter.setData(item.getRelationId(), itemRelation);
            }
        });
    }

    private void initData() {
//        for (int i = 0; i < 50; i++) {
//            mDataList.add("旧条目_" + i);
//        }
//        mAdapter.setNewData(mDataList);

        for (int i = 0; i < 50; i++) {
            MultiItemBean itemBean = null;
            if (i % 2 == 0) {
                itemBean = new MultiItemBean(ViewType.TEXT.ordinal(), "文本", "旧文本" + i);
                int relationId = (int) i / 2;
                itemBean.setRelationId(relationId);
            } else {
                itemBean = new MultiItemBean(ViewType.EDITTEXT.ordinal(), "编辑框", "编辑框" + i);
                int relationId = (int) i / 2;
                itemBean.setRelationId(relationId);
            }
            mMultiDataList.add(itemBean);
        }
        mBRVAHMultiRvAdapter.setNewData(mMultiDataList);
    }
}
